package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    public ClazzMapper clazzMapper;


    @Override
    public PageResult<Student> findbyPage(StudentQueryParam student) {
        //设置分页参数
        IPage<Student> studentPage = new Page<>(student.getPage(), student.getPageSize());
        //执行查询只有分页条件，没有查询条件
        LambdaQueryWrapper<Student> eq = new LambdaQueryWrapper<Student>()
                .like(StringUtils.isEmpty(student.getName()), Student::getName, student.getName())
                .eq(student.getDegree() != null, Student::getDegree, student.getDegree())
                .eq(student.getClazzId() != null, Student::getClazzId, student.getClazzId());
        page(studentPage, eq);
        //处理班级的展示
        List<Student> students = studentPage.getRecords();
        students.forEach(student1 -> {
            Clazz clazz = clazzMapper.selectById(student1.getClazzId());
            if (clazz != null){
                student1.setClazzName(clazz.getName());
            }
        });
       // 封装结果
        return new PageResult<>(studentPage.getTotal(), students);
    }
}
